﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

namespace debioscoopincdrie.Managers
{
    class OutputManager
    {
        #region Fields
        private static OutputManager INSTANCE;
        #endregion

        #region Constructors
        private OutputManager()
        {
            
        }
        #endregion

        #region Static functions
        public static OutputManager GetManager()
        {
            if (INSTANCE == null)
            {
                INSTANCE = new OutputManager();
            }

            return INSTANCE;
        }
        #endregion

        public void createBon(double price)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/bon.pdf";

            Dictionary<string, string> replaceStuff = new Dictionary<string, string>();
            replaceStuff.Add("prijs", price.ToString("0.00"));
            replaceStuff.Add("van", DateTime.Today.ToString("dd-MM-yyyy"));
            replaceStuff.Add("tot", DateTime.Today.AddYears(1).ToString("dd-MM-yyyy"));
            replaceStuff.Add("code", new Random().Next(100000000, 999999999).ToString());


            Document document = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
            try
            {
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("http://data.kazuhiko.nl/html/ticket.png");
                png.Alignment = iTextSharp.text.Image.UNDERLYING;
                PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
                document.Open();
                WebClient wc = new WebClient();
                string htmlText = wc.DownloadString("http://data.kazuhiko.nl/html/bon.html");
                foreach (string key in replaceStuff.Keys)
                {
                    htmlText = htmlText.Replace("$" + key, replaceStuff[key]);
                }
                document.Add(png);
                List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);
                for (int k = 0; k < htmlarraylist.Count; ++k)
                {
                    document.Add((IElement)htmlarraylist[k]);
                }

                wc.Dispose();
                document.Close();
            }
            catch
            { }
        }

        public void createAbonnement( String naam, int korting, DateTime eind )
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/abbo-" + naam + ".pdf";

            Dictionary<string, string> replaceStuff = new Dictionary<string, string>();
            replaceStuff.Add("abnaam", naam);
            replaceStuff.Add("korting", korting.ToString("0") );
            if (eind.Date == DateTime.Today.Date)
            {
                replaceStuff.Add("vanaf", "");
                replaceStuff.Add("tot", "");
            }
            else
            {
                replaceStuff.Add("vanaf", DateTime.Today.ToString("dd-MM-yyyy"));
                replaceStuff.Add("tot", eind.ToString("dd-MM-yyyy"));
            }
            replaceStuff.Add("nummer", new Random().Next(10000).ToString());

            Document document = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
            try
            {
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("http://data.kazuhiko.nl/html/ticket.png");
                png.Alignment = iTextSharp.text.Image.UNDERLYING;
                PdfWriter.GetInstance(document, new FileStream( path, FileMode.Create));
                document.Open();
                WebClient wc = new WebClient();
                string htmlText = wc.DownloadString("http://data.kazuhiko.nl/html/abonnement.html");
                foreach( string key in replaceStuff.Keys)
                {
                    htmlText = htmlText.Replace("$" + key, replaceStuff[key]);
                }
                if (eind.Date == DateTime.Today.Date)
                {
                    htmlText = htmlText.Replace("tot", "");
                }
                document.Add(png);
                List<IElement> htmlarraylist=  HTMLWorker.ParseToList(new StringReader(htmlText), null);
                for( int k = 0; k < htmlarraylist.Count; ++k)
                {
                    document.Add((IElement)htmlarraylist[k]);
                }

                wc.Dispose();
                document.Close();
            }
            catch
            {
            }
        }

        public void createTicket( int reservationId, int ticketId )
        {
            bestelling reservation = ReservationManager.GetManager().getReservation(reservationId);

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/ticket-" + reservationId + "-" + ticketId + ".pdf";
            
            Dictionary<string, string> replaceStuff = new Dictionary<string,string>();
            replaceStuff.Add( "zaal", reservation.planning.zaal.zaalnummer.ToString());
            replaceStuff.Add( "film_naam", reservation.planning.film.naam );
            replaceStuff.Add( "datum", reservation.planning.datum.ToString( "ddd dd-MM-yyy") );
            replaceStuff.Add( "tijd", reservation.planning.tijd.ToString( @"hh\:mm" ) );
            replaceStuff.Add("prijs", reservation.kaartje.Where( b => b.kaartjeid == ticketId ).ToList()[0].prijs.ToString( "0.00"));
            replaceStuff.Add("ticket_id", ticketId.ToString());

            Document document = new Document( PageSize.A4, 0f, 0f, 0f, 0f);
            try
            {
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("http://data.kazuhiko.nl/html/ticket.png");
                png.Alignment = iTextSharp.text.Image.UNDERLYING;
                PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
                document.Open();
                WebClient wc = new WebClient();
                string htmlText = wc.DownloadString("http://data.kazuhiko.nl/html/ticket.html");
                foreach (string key in replaceStuff.Keys)
                {
                    htmlText = htmlText.Replace("$" + key, replaceStuff[key]);
                }
                document.Add(png);
                List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);
                for (int k = 0; k < htmlarraylist.Count; ++k)
                {
                    document.Add((IElement)htmlarraylist[k]);
                }

                wc.Dispose();
                document.Close();
            }
            catch
            {
            }
        }
    }
}
